package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.service.CheckItemService;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



/**
 * @author hql
 * @date 2019-12-01 12:53
 */
@RestController
@RequestMapping("/checkItem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;


    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        boolean flag = false;
        String message=null;
        try {
            checkItemService.add(checkItem);
            flag=true;
            message= MessageConstant.ADD_CHECKGROUP_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            message=MessageConstant.ADD_CHECKGROUP_FAIL;
        }


        return new Result(flag,message);
    }

        @RequestMapping("/find")
        public Result find(@RequestBody QueryPageBean queryPageBean){
            boolean flag=false;
            String message=null;

            PageResult pageResult= null;
            try {
                pageResult = checkItemService.find(queryPageBean);
                flag=true;
                message=MessageConstant.QUERY_CHECKITEM_SUCCESS;

            } catch (Exception e) {
                e.printStackTrace();
                message=MessageConstant.QUERY_CHECKITEM_FAIL;
            }


            return  new Result(flag,message,pageResult);
        }

                     @RequestMapping("/findById")
                     public Result findById(@Param("code") String code){
                         boolean flag=false;
                         String message=null;
                         CheckItem checkItem=null;
                         try {
                             checkItem=  checkItemService.findById(code);
                             flag=true;
                             message=MessageConstant.EDIT_CHECKITEM_SUCCESS;
                         } catch (Exception e) {
                             e.printStackTrace();
                             message=MessageConstant.EDIT_CHECKITEM_FAIL;
                         }
                         return new Result(flag,message,checkItem);

    }

        @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")//权限校验
        @RequestMapping("/updateCheckItem")
            public Result updateCheckItem(@RequestBody CheckItem checkItem){
                boolean flag=false;
                String message=null;
                try {
                    checkItemService.updateCheckItem(checkItem);
                    flag=true;
                    message=MessageConstant.UPLOAD_SUCCESS;

                } catch (Exception e) {
                    e.printStackTrace();
                    message=MessageConstant.ADD_CHECKITEM_FAIL;
                }
                return new Result(flag,message);
        }
        @RequestMapping("/delById")
        public Result delById(Integer id){
            boolean flag=false;
            String message=null;
            try {
                checkItemService.delById(id);
                flag=true;
                message=MessageConstant.UPLOAD_SUCCESS;
            }catch (RuntimeException r){
                message=r.getMessage();
            }catch (Exception e) {
                e.printStackTrace();
                message=MessageConstant.DELETE_CHECKITEM_FAIL;
            }


            return new Result(flag,message);
        }
}
